
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/testing/pytest.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:09 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>pytest &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../live.sympy.org/static/live-sphinx.js"></script>
    
    <link rel="shortcut icon" href="../../_static/sympy-notailtext-favicon.ico"/>
    <link href="pytest.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Randomised Testing" href="randtest.html" />
    <link rel="prev" title="Testing" href="index.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="randtest.html" title="Randomised Testing"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="index.html" title="Testing"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" accesskey="U">Testing</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">pytest</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="module-sympy.testing.pytest">
<span id="pytest"></span><h1>pytest<a class="headerlink" href="#module-sympy.testing.pytest" title="Permalink to this headline">¶</a></h1>
<p>py.test hacks to support XFAIL/XPASS</p>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.testing.pytest.SKIP">
<span class="sig-prename descclassname"><span class="pre">sympy.testing.pytest.</span></span><span class="sig-name descname"><span class="pre">SKIP</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">reason</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/testing/pytest.py#L158-L167"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.testing.pytest.SKIP" title="Permalink to this definition">¶</a></dt>
<dd><p>Similar to <code class="docutils literal notranslate"><span class="pre">skip()</span></code>, but this is a decorator.</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.testing.pytest.ignore_warnings">
<span class="sig-prename descclassname"><span class="pre">sympy.testing.pytest.</span></span><span class="sig-name descname"><span class="pre">ignore_warnings</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">warningcls</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/testing/pytest.py#L263-L307"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.testing.pytest.ignore_warnings" title="Permalink to this definition">¶</a></dt>
<dd><p>Context manager to suppress warnings during tests.</p>
<p>This function is useful for suppressing warnings during tests. The warns
function should be used to assert that a warning is raised. The
ignore_warnings function is useful in situation when the warning is not
guaranteed to be raised (e.g. on importing a module) or if the warning
comes from third-party code.</p>
<p>When the warning is coming (reliably) from SymPy the warns function should
be preferred to ignore_warnings.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.testing.pytest</span> <span class="kn">import</span> <span class="n">ignore_warnings</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">warnings</span>
</pre></div>
</div>
<p>Here’s a warning:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">warnings</span><span class="o">.</span><span class="n">catch_warnings</span><span class="p">():</span>  <span class="c1"># reset warnings in doctest</span>
<span class="gp">... </span>    <span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s1">&#39;error&#39;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;deprecated&#39;</span><span class="p">,</span> <span class="ne">UserWarning</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">UserWarning</span>: <span class="n">deprecated</span>
</pre></div>
</div>
<p>Let’s suppress it with ignore_warnings:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">warnings</span><span class="o">.</span><span class="n">catch_warnings</span><span class="p">():</span>  <span class="c1"># reset warnings in doctest</span>
<span class="gp">... </span>    <span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s1">&#39;error&#39;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">with</span> <span class="n">ignore_warnings</span><span class="p">(</span><span class="ne">UserWarning</span><span class="p">):</span>
<span class="gp">... </span>        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;deprecated&#39;</span><span class="p">,</span> <span class="ne">UserWarning</span><span class="p">)</span>
</pre></div>
</div>
<p>(No warning emitted)</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.testing.pytest.nocache_fail">
<span class="sig-prename descclassname"><span class="pre">sympy.testing.pytest.</span></span><span class="sig-name descname"><span class="pre">nocache_fail</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/testing/pytest.py#L179-L181"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.testing.pytest.nocache_fail" title="Permalink to this definition">¶</a></dt>
<dd><p>Dummy decorator for marking tests that fail when cache is disabled</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.testing.pytest.raises">
<span class="sig-prename descclassname"><span class="pre">sympy.testing.pytest.</span></span><span class="sig-name descname"><span class="pre">raises</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expectedException</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">code</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/testing/pytest.py#L43-L114"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.testing.pytest.raises" title="Permalink to this definition">¶</a></dt>
<dd><p>Tests that <code class="docutils literal notranslate"><span class="pre">code</span></code> raises the exception <code class="docutils literal notranslate"><span class="pre">expectedException</span></code>.</p>
<p><code class="docutils literal notranslate"><span class="pre">code</span></code> may be a callable, such as a lambda expression or function
name.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">code</span></code> is not given or None, <code class="docutils literal notranslate"><span class="pre">raises</span></code> will return a context
manager for use in <code class="docutils literal notranslate"><span class="pre">with</span></code> statements; the code to execute then
comes from the scope of the <code class="docutils literal notranslate"><span class="pre">with</span></code>.</p>
<p><code class="docutils literal notranslate"><span class="pre">raises()</span></code> does nothing if the callable raises the expected exception,
otherwise it raises an AssertionError.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.testing.pytest</span> <span class="kn">import</span> <span class="n">raises</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">raises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">,</span> <span class="k">lambda</span><span class="p">:</span> <span class="mi">1</span><span class="o">/</span><span class="mi">0</span><span class="p">)</span>
<span class="go">&lt;ExceptionInfo ZeroDivisionError(...)&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">raises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">,</span> <span class="k">lambda</span><span class="p">:</span> <span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">Failed</span>: <span class="n">DID NOT RAISE</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">raises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">n</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="mi">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">raises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">n</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="mi">2</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">Failed</span>: <span class="n">DID NOT RAISE</span>
</pre></div>
</div>
<p>Note that you cannot test multiple statements via
<code class="docutils literal notranslate"><span class="pre">with</span> <span class="pre">raises</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">raises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">n</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="mi">0</span>    <span class="c1"># will execute and raise, aborting the ``with``</span>
<span class="gp">... </span>    <span class="n">n</span> <span class="o">=</span> <span class="mi">9999</span><span class="o">/</span><span class="mi">0</span> <span class="c1"># never executed</span>
</pre></div>
</div>
<p>This is just what <code class="docutils literal notranslate"><span class="pre">with</span></code> is supposed to do: abort the
contained statement sequence at the first exception and let
the context manager deal with the exception.</p>
<p>To test multiple statements, you’ll need a separate <code class="docutils literal notranslate"><span class="pre">with</span></code>
for each:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">raises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">n</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="mi">0</span>    <span class="c1"># will execute and raise</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">raises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">n</span> <span class="o">=</span> <span class="mi">9999</span><span class="o">/</span><span class="mi">0</span> <span class="c1"># will also execute and raise</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.testing.pytest.warns">
<span class="sig-prename descclassname"><span class="pre">sympy.testing.pytest.</span></span><span class="sig-name descname"><span class="pre">warns</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">warningcls</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">match</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/testing/pytest.py#L183-L214"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.testing.pytest.warns" title="Permalink to this definition">¶</a></dt>
<dd><p>Like raises but tests that warnings are emitted.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.testing.pytest</span> <span class="kn">import</span> <span class="n">warns</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">warnings</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">warns</span><span class="p">(</span><span class="ne">UserWarning</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;deprecated&#39;</span><span class="p">,</span> <span class="ne">UserWarning</span><span class="p">)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">warns</span><span class="p">(</span><span class="ne">UserWarning</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">pass</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">Failed</span>: <span class="n">DID NOT WARN. No warnings of type UserWarning        was emitted. The list of emitted warnings is: [].</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.testing.pytest.warns_deprecated_sympy">
<span class="sig-prename descclassname"><span class="pre">sympy.testing.pytest.</span></span><span class="sig-name descname"><span class="pre">warns_deprecated_sympy</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/testing/pytest.py#L238-L261"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.testing.pytest.warns_deprecated_sympy" title="Permalink to this definition">¶</a></dt>
<dd><p>Shorthand for <code class="docutils literal notranslate"><span class="pre">warns(SymPyDeprecationWarning)</span></code></p>
<p>This is the recommended way to test that <code class="docutils literal notranslate"><span class="pre">SymPyDeprecationWarning</span></code> is
emitted for deprecated features in SymPy. To test for other warnings use
<code class="docutils literal notranslate"><span class="pre">warns</span></code>. To suppress warnings without asserting that they are emitted
use <code class="docutils literal notranslate"><span class="pre">ignore_warnings</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.testing.pytest</span> <span class="kn">import</span> <span class="n">warns_deprecated_sympy</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.exceptions</span> <span class="kn">import</span> <span class="n">SymPyDeprecationWarning</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">warns_deprecated_sympy</span><span class="p">():</span>
<span class="gp">... </span>    <span class="n">SymPyDeprecationWarning</span><span class="p">(</span><span class="s2">&quot;Don&#39;t use&quot;</span><span class="p">,</span> <span class="n">feature</span><span class="o">=</span><span class="s2">&quot;old thing&quot;</span><span class="p">,</span>
<span class="gp">... </span>        <span class="n">deprecated_since_version</span><span class="o">=</span><span class="s2">&quot;1.0&quot;</span><span class="p">,</span> <span class="n">issue</span><span class="o">=</span><span class="mi">123</span><span class="p">)</span><span class="o">.</span><span class="n">warn</span><span class="p">()</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">warns_deprecated_sympy</span><span class="p">():</span>
<span class="gp">... </span>    <span class="k">pass</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">Failed</span>: <span class="n">DID NOT WARN. No warnings of type     SymPyDeprecationWarning was emitted. The list of emitted warnings is: [].</span>
</pre></div>
</div>
</dd></dl>

</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h4>Previous topic</h4>
  <p class="topless"><a href="index.html"
                        title="previous chapter">Testing</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="randtest.html"
                        title="next chapter">Randomised Testing</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/modules/testing/pytest.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="randtest.html" title="Randomised Testing"
             >next</a> |</li>
        <li class="right" >
          <a href="index.html" title="Testing"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" >Testing</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">pytest</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/testing/pytest.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:10 GMT -->
</html>